#define _CRT_SECURE_NO_WARNINGS	
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;


template<class K>
struct AVLTreeNode
{
	K _key;
	struct AVLTreeNode<K>* _left;
	struct AVLTreeNode<K>* _right;
	struct AVLTreeNode<K>* _parent;
	int _bf;

	AVLTreeNode(const K& key)
		:_key(key)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}

};

template<class K>
class AVLTree
{
	typedef AVLTreeNode<K> Node;
public:
	bool Insert(const K& key)
	{
		if (_root==nullptr)
		{
			_root = new Node(key);
			return true;
		}
		
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					assert(false);
				}
			}
			cur = new Node(key);
			if (cur->_key < parent->_key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}
			cur->_parent = parent;
		

		//调整平衡因子：
		while (parent)
		{
			if (parent->_left == cur)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf==1||parent->_bf==-1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf==-2||parent->_bf==2)
			{
				//旋转：
				if (parent->_bf ==2 && cur->_bf == 1)//单纯右边高左旋
				{
					RotateL(parent);
				}
				else if (parent->_bf==-2&& cur->_bf==-1)//单纯左边高右旋
				{
					RotateR(parent);
				}
				else if (parent->_bf==2&&cur->_bf==-1)//右左双旋
				{
					RotateRL(parent);
				}
				else if (parent->_bf==-2&&cur->_bf==1)//左右双旋
				{
					RotateLR(parent);
				}
				else
				{
					assert(false);
				}
				break;
			}
			else
			{
				assert(false);
			}
		}
		return true;
	}

	//四种旋转方法的实现：
	void RotateL(Node* parent)
	{
		Node* SubR = parent->_right;
		Node* SubRL = SubR->_left;
		Node* pparent = parent->_parent;

		
		parent->_right = SubRL;
		if (SubRL)
		{
			SubRL->_parent = parent;//这里要判断SubRL是否为空否则会有空指针的解引用
		}
		SubR->_left = parent;
		parent->_parent = SubR;
		if (parent == _root)
		{
			_root = SubR;
			SubR->_parent == nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = SubR;
			}
			else
			{
				pparent->_right = SubR;
			}
			SubR->_parent = pparent;
		}

		//调整平衡因子
		SubR->_bf = 0;
		parent->_bf = 0;
	}
	void RotateR(Node* parent)
	{
		Node* SubL = parent->_left;
		Node* SubLR = SubL->_right;
		Node* pparent = parent->_parent;


		parent->_right = SubLR;
		if (SubLR)
		{
			SubLR->_parent = parent;//这里要判断SubRL是否为空否则会有空指针的解引用
		}
		SubL->_right = parent;
		parent->_parent = SubL;
		if (parent == _root)
		{
			_root = SubL;
			SubL->_parent == nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = SubL;
			}
			else
			{
				pparent->_right = SubL;
			}
			SubL->_parent = pparent;
		}

		//调整平衡因子
		SubL->_bf = 0;
		parent->_bf = 0;
	}
	void RotateRL(Node* parent)
	{
		Node* SubR = parent->_right;
		Node* SubRL = SubR->_left;
		int bf = SubRL->_bf;
		RotateR(parent->_right);
		RotateL(parent);

		//调整平衡因子：
		if (bf==-1)
		{
			parent->_bf = 0;
			SubR->_bf = 1;
			SubRL->_bf = 0;
		}
		else if (bf==0)
		{
			parent->_bf = 0;
			SubR->_bf = 0;
			SubRL->_bf = 0;
		}
		else if (bf == 11)
		{
			parent->_bf = -1;
			SubR->_bf = 0;
			SubRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	void RotateLR(Node* parent)
	{
		Node* SubL = parent->_left;
		Node* SubLR = SubL->_right;
		int bf = SubLR->_bf;
		RotateL(parent->_left);
		RotateR(parent);

		//调整平衡因子：
		if (bf == -1)
		{
			parent->_bf = 1;
			SubL->_bf = 0;
			SubLR->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			SubL->_bf = 0;
			SubLR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			SubL->_bf = -1;
			SubLR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
private:
	Node* _root;
};
Gitee - 基于 Git 的代码托管和研发协作平台
